package MyIOTest;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test01 {
    public static void main(String[] args) throws IOException {
        //姓氏网站
        String FamilyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        //男生名字网站
        String BoyNameNet = "https://www.mingziwang.cn/baobaoqiming/33951.html";
        //女生名字网站
        String GirlNameNet = "https://www.xingzuo.com/qiming/detail/235708.html";
        //分别进入网站爬取
        String FamilyName = WebCrawLer(FamilyNameNet);
        String BoyName = WebCrawLer(BoyNameNet);
        String GirlName = WebCrawLer(GirlNameNet);
        //System.out.println(FamilyName);
        //System.out.println(BoyName);
        //System.out.println(GirlName);
        //将得到的数据进行切割获得真正的名字
        ArrayList<String> l1 = getData(FamilyName, "(.{4})(，|。)", 1);
        ArrayList<String> l2 = getData(BoyName, "([\\u4E00-\\u9FA5]{2})(、|。)", 1);
        ArrayList<String> l3 = getData(GirlName, "([\\u4E00-\\u9FA5]{2})(、|。)", 1);
        //System.out.println(l3);
        ArrayList<String> FamilyNameList = new ArrayList<>();
        for (String s : l1) {
            for (int i = 0; i < s.length(); i++) {
                //将每个姓氏拿出加入姓氏集合
                FamilyNameList.add(String.valueOf(s.charAt(i)));
            }
        }
        //System.out.println(FamilyNameList);
        ArrayList<String> BoyNameList = new ArrayList<>();
        for (String s : l2) {
            //去重并且加入名字
            if(!BoyNameList.contains(s)){
                BoyNameList.add(s);
            }
        }
        //System.out.println(BoyNameList);
        ArrayList<String> GirlNameList = new ArrayList<>();
        for (String s : l3) {
            //去重并且加入名字
            if(!GirlNameList.contains(s)){
                GirlNameList.add(s);
            }
        }
        //System.out.println(GirlNameList);
        ArrayList<String> name = toName(FamilyNameList, BoyNameList, GirlNameList, 5, 5);
        Collections.shuffle(name);
        //将男女生顺序打乱并打印
        System.out.println(name);

        BufferedWriter bw = new BufferedWriter(new FileWriter("allname.txt"));
        for (String s : name) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }
    public static ArrayList<String> toName(ArrayList<String> FamilyNameList,ArrayList<String> BoyNameList,ArrayList<String> GirlNameList,int boyCount,int girlCount){
        HashSet<String> boy = new HashSet();
        //使用hashset去重
        while (true){
            if(boy.size() == boyCount){
                //人数够了就停止
                break;
            }
            //将姓氏打乱
            Collections.shuffle(FamilyNameList);
            //将名字打乱
            Collections.shuffle(BoyNameList);
            //分别取零索引的随机值
            boy.add(FamilyNameList.get(0) + BoyNameList.get(0));
        }
        //使用hashset去重
        HashSet<String> girl = new HashSet();
        while (true){
            if(girl.size() == girlCount){
                //人数够了就停止
                break;
            }
            //将姓氏打乱
            Collections.shuffle(FamilyNameList);
            //将名字打乱
            Collections.shuffle(GirlNameList);
            //分别取零索引的随机值
            girl.add(FamilyNameList.get(0) + GirlNameList.get(0));
        }
        //对男女生的年龄任意
        Random r = new Random();
        ArrayList<String> list = new ArrayList<>();
        for (String s : boy) {
            //s表示男生的名字
            list.add(s + "-男-" + (r.nextInt(10) + 18));
        }
        for (String s : girl) {
            //s表示女生的名字
            list.add(s + "-女-" + (r.nextInt(10) + 18));
        }
        return list;
    }
    public static ArrayList<String> getData(String str,String regex,int index){
        //创建list集合用于返回
        ArrayList<String> list = new ArrayList<>();
        //定义一个规则用来爬取
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()){
            //添加第一组索引的值，即只有名字
            String group = matcher.group(index);
            //将名字添加集合
            list.add(group);
        }
        return list;
    }
    public static String WebCrawLer(String web) throws IOException {
        URL url = new URL(web);
        //对指定网站进入
        URLConnection conn = url.openConnection();
        //创建读取的对象
        InputStreamReader isr = new InputStreamReader(conn.getInputStream());
        int ch;
        StringBuilder sb = new StringBuilder();
        //用来拼接数据
        while ((ch = isr.read()) != -1){
            //挨个读取
            sb.append((char) ch);
        }
        isr.close();
        //释放资源
        return sb.toString();
    }
}
